﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using System.ComponentModel;
using NUnit.Framework;
using EasyProp;

namespace EasyProp.Tests
{
    [TestFixture]
    public class MarkObjectAsDirtyFixture
    {
        [SetUp]
        public void SetUp()
        {
            _easyPropBuilder = new EasyPropBuilder();
        }

        [Test]
        public void should_set_is_dirty_to_true_when_property_changes()
        {
            ClassWithMarkAsDirtyFilter theObject = _easyPropBuilder.Build<ClassWithMarkAsDirtyFilter>();

            Assert.IsFalse(theObject.IsDirty, "Should not be dirty initially.");
            theObject.MyProperty = "blablabla";
            Assert.IsTrue(theObject.IsDirty, "Should be dirty");
        }

        [Test]
        public void should_do_nothing_if_no_is_dirty_property_exists()
        {
            ClassWithoutIsDirtyProperty theObject = _easyPropBuilder.Build<ClassWithoutIsDirtyProperty>();
            theObject.MyProperty = "blablabla";
        }

        [Test]
        public void should_handle_isdirty_virtual_and_self_assigned()
        {
            ClassWithVirtualIsDirtyProperty theObject = _easyPropBuilder.Build<ClassWithVirtualIsDirtyProperty>();

            theObject.MyProperty = "blablabla";            
            Assert.IsTrue(theObject.IsDirty, "Should be dirty after property set");

            theObject.IsDirty = false;
            Assert.IsFalse(theObject.IsDirty, "IsDirty should be false after explicit set.");
        }

        EasyPropBuilder _easyPropBuilder;

        [AfterPropertySetFilter(typeof(MarkObjectAsDirty))]
        public class ClassWithMarkAsDirtyFilter
        {
            public bool IsDirty { get; set; }
            public virtual string MyProperty { get; set; }
        }

        [AfterPropertySetFilter(typeof(MarkObjectAsDirty))]
        public class ClassWithoutIsDirtyProperty 
        {
            public virtual string MyProperty { get; set; }
        }

        [AfterPropertySetFilter(typeof(MarkObjectAsDirty))]
        public class ClassWithVirtualIsDirtyProperty
        {
            public virtual bool IsDirty { get; set; }
            public virtual string MyProperty { get; set; }
        }
    }
}
